
<html>
<head>
<title>MiBand 5 & 6 watchface editor GUI</title>
</head>
<body>
<h1><a onclick="download()" name="download_button">Download<h1>
</body>
<script>
  fetch("https://api.github.com/repos/Johnson070/MiBand-5-watchface-editor/releases").then(
    function(response){
      return response.json()
    }).then(function(data){
      var p = data.map(function(x){
       if(typeof x.assets == 'undefined' || x.assets.length == 0){ return 0; }
        sumDownloads = 0;
        for (var i=0; i < x.assets.length;i++) {
            sumDownloads += x.assets[i].download_count;
            if (document.getElementsByName("download_button")[0].href.indexOf("github") == -1) {
                document.getElementsByName("download_button")[0].href = x.assets[i].browser_download_url;
            }
        }
        return sumDownloads;
      });

      var b = data.map(function(x){
        if(typeof x.tag_name == 'undefined' || x.tag_name.length == 0){ return 0; }
        return x.tag_name;
      });

      var sum = 0;
      for(var i=0;i<p.length;i++){
        sum += p[i];
      }
      var newUpdate = p[0];

      document.querySelector('body').innerHTML += `<h2>Total downloads: <font color="red">${sum}</font></h2>`;
        
      for (var i = 0; i < data.length;i++)
      {
        if(typeof data[i].assets == 'undefined' || data[i].assets.length == 0){ continue; }
        sumDownloads = 0;
        for (var j=0; j < data[i].assets.length;j++)
            sumDownloads += data[i].assets[j].download_count;
        document.querySelector('body').innerHTML += `<h3>${b[i]} Downloads: <font color="red">${sumDownloads}</font></h3>${
            (new Date(data[i].published_at)).toString().substring(4,(new Date(data[i].published_at)).toString().indexOf("GMT"))}`;
      }
    });

    function download(){
        var p = data.map(function(x){
       if(typeof x.assets == 'undefined' || x.assets.length == 0){ return 0; }
        sumDownloads = 0;
        for (var i=0; i < x.assets.length;i++) {
            sumDownloads += x.assets[i].download_count;
            if (document.getElementsByName("download_button")[0].href.indexOf("github") == -1) {
                document.getElementsByName("download_button")[0].href = x.assets[i].browser_download_url;
            }
        }
        return sumDownloads;
      });
    }
</script>
</html>
